package com.holystone.orca.sys.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.holystone.orca.base.service.impl.BaseServiceImpl;
import com.holystone.orca.common.PageData;
import com.holystone.orca.sys.model.StaffVO;
import com.holystone.orca.sys.mybatis.dao.SysPersonMapper;
import com.holystone.orca.sys.mybatis.model.SysPerson;
import com.holystone.orca.sys.service.ILoginService;
import com.holystone.orca.sys.service.IStaffPrivilegeService;
import com.holystone.orca.sys.service.IStaffRoleService;
import com.holystone.orca.sys.service.ISysPersonService;
@Service("sysPersonService")
public class SysPersonServiceImpl extends BaseServiceImpl<SysPerson> implements ISysPersonService{
private static Logger logger = Logger.getLogger(SysLookupCodeServiceImpl.class);
	
	/**dao层服务*/
	@Autowired
	private SysPersonMapper sysPersonMapper;
	
	@Autowired
	private IStaffPrivilegeService staffPrivilegeService;
	
	@Autowired
	private IStaffRoleService staffRoleService;
	
	@Autowired
	private ILoginService loginService;
	
	public PageData pagingSysPerson( int iPageNo, int iPageSize,String depId,String personName) {
		Map<Object, String> map=new HashMap<Object, String>();
		map.put("personName", personName);
		map.put("depId", depId);
		//map.put("personPassword",personPassword);
		//map.put("staffState",SystemConstant.STAFF_STATE_A);
		
        //分页查询
        PageHelper.startPage(iPageNo, iPageSize);
        PageInfo<StaffVO> pageInfo = new PageInfo<StaffVO>(sysPersonMapper.getStaffVO(map));
		PageData pageData =  new PageData();
		pageData.setPageList(pageInfo.getList());
		pageData.setTotalCount(pageInfo.getTotal());
		pageData.setPageSize(pageInfo.getPageSize());
		pageData.setPageNo(pageInfo.getPageNum());
		pageData.setTotalPage(pageInfo.getPages());
		return pageData;
	}
	
	public SysPerson selectByPersonId(String personId){
		//map.put("personPassword",personPassword);
		//map.put("staffState",SystemConstant.STAFF_STATE_A);
		List<SysPerson> pList = sysPersonMapper.selectPerson(personId);
		if(pList!=null && pList.size()==1){
			SysPerson vo = pList.get(0);
//			{//初始化员工权限
//				List<SysPrivilege> ls=this.staffPrivilegeService.getStaffPrivilegeList(vo.getStaffId());
//				vo.setPrivilegeList(ls);
//			}
//			{//初始化员工角色
//				List<SysRole> ls=this.staffRoleService.getStaffRoleList(vo.getStaffId());
//				vo.setRoleList(ls);
//			}
			return vo;
		}else{
			return null;
		}
	}

	@Override
	public boolean insertPerson(SysPerson sysPerson) throws Exception {
		int result=sysPersonMapper.insertSysPerson(sysPerson);
		if(result==1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean updatePerson(SysPerson sysPerson) throws Exception {
		int result=sysPersonMapper.updateSysPerson(sysPerson);
		if(result==1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean deletePersonByPersonId(int personId) throws Exception {
		int result=sysPersonMapper.deletePersonByPersonId(personId);
		if(result==1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean delSelectPerson(List<Integer> list) throws Exception {
		int result=sysPersonMapper.delSelectPerson(list);
		if(result==list.size()){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public SysPerson selectPersonByPersonCode(String personCode)
			throws Exception {
		SysPerson sysPerson = sysPersonMapper.selectPersonByPersonCode(personCode);
		return sysPerson;
	}

	@Override
	public StaffVO selectStaffVOByPersonCode(Map<Object,String> map)
			throws Exception {
		List<StaffVO> staffVOList = sysPersonMapper.getStaffVO(map);
		if(staffVOList.size() == 0){
			return null;
		}else{
			return staffVOList.get(0);
		}
	}

	@Override
	public boolean editPassword(Map<Object, String> map) throws Exception {
		int result = sysPersonMapper.editPassword(map);
		if(result == 1){
			return true;
		}else{
			return false;
		}
	}
}
